﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Diagnostics;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for ErrorLog
/// </summary>
public class ErrorLog
{
    public ErrorLog()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void Log(Exception exception, Label lblErrorMessage)
    {
        /*-----------------------------------------------------------------------------------------------
        Function Name   : Log
        Arguments	    : exception, label for displaying the message.
        Return Type     : void
        Description	    : Logs the Exception in a log file which is generated in the name of the aspx page
                           Displays a standard error message in the label passed as argument
        -----------------------------------------------------------------------------------------------*/
        string strFileName = DateTime.Today.ToString("dddd, MMMM dd, yyyy");
        string strErrorLogPath = (HttpContext.Current.Server.MapPath(@"~\ErrorLogs\") + (strFileName + ".log"));
        string strErrorSrcFileName = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.CurrentExecutionFilePath);
        string strMethodName = new StackTrace().GetFrame(1).GetMethod().Name;
        string strUserName = HttpContext.Current.Session["ssnLoginName"] == null ? " " : HttpContext.Current.Session["ssnLoginName"] as string;
        string strErrorMessage;


        TextWriter twErrorFile;
        strErrorMessage = (DateTime.Now.ToString() + "\r\n"); ;
        strErrorMessage += "File Name     :" + strErrorSrcFileName + "\r\n";
        strErrorMessage += "Method Name   :" + strMethodName + "\r\n";
        strErrorMessage += "Error Text    :" + exception.Message + "\r\n";
        strErrorMessage += "Error Desc    :" + exception.StackTrace + "\r\n";
        strErrorMessage += "User Name     :" + strUserName + "\r\n";
        strErrorMessage += "-----------------------------------------------------------------------------------------------------------------------------------------------" + "\r\n";
        twErrorFile = File.AppendText(strErrorLogPath);
        twErrorFile.WriteLine(strErrorMessage);
        twErrorFile.Close();

        // Display a standard error message on the screen
        //if (System.Configuration.ConfigurationManager.AppSettings["ShowErrorsinUI"].ToString() == "1")
        //{
        //    lblErrorMessage.Text = "Server Error. Please try later.";
        //}
    }

    public void Log(Exception exception, System.Web.UI.Page page)
    {
        /*-----------------------------------------------------------------------------------------------
        Function Name   : Log
        Arguments	    : exception
        Return Type     : void
        Description	    : Logs the Exception in a log file which is generated in the name of the aspx page
                          Displays a standard error message in Master Page header
        -----------------------------------------------------------------------------------------------*/
        string strFileName = DateTime.Today.ToString("dddd, MMMM dd, yyyy");
        string strErrorLogPath = (HttpContext.Current.Server.MapPath(@"~\ErrorLogs\") + (strFileName + ".log"));
        string strErrorSrcFileName = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.CurrentExecutionFilePath);
        string strMethodName = new StackTrace().GetFrame(1).GetMethod().Name;
        string strUserName = HttpContext.Current.Session["ssnLoginName"] == null ? " " : HttpContext.Current.Session["ssnLoginName"] as string;
        string strErrorMessage;

        TextWriter twErrorFile;
        strErrorMessage = (DateTime.Now.ToString() + "\r\n"); ;
        strErrorMessage += "File Name     :" + strErrorSrcFileName + "\r\n";
        strErrorMessage += "Method Name   :" + strMethodName + "\r\n";
        strErrorMessage += "Error Text    :" + exception.Message + "\r\n";
        strErrorMessage += "Error Desc    :" + exception.StackTrace + "\r\n";
        strErrorMessage += "User Name     :" + strUserName + "\r\n";
        strErrorMessage += "-----------------------------------------------------------------------------------------------------------------------------------------------" + "\r\n";
        twErrorFile = File.AppendText(strErrorLogPath);
        twErrorFile.WriteLine(strErrorMessage);
        twErrorFile.Close();

        // Display a standard error message on the screen
        //if (System.Configuration.ConfigurationManager.AppSettings["ShowErrorsinUI"].ToString() == "1")
        //{
        //    ((Label)page.Master.FindControl("lblExceptionMessage")).Visible = true;
        //    ((Label)page.Master.FindControl("lblExceptionMessage")).Text = "Server Error. Please try later.";
        //}
    }

    public void LogGlobal(Exception exception)
    {
        /*-----------------------------------------------------------------------------------------------
        Function Name   : LogGlobal
        Arguments	    : exception
        Return Type     : void
        Description	    : Logs Global Exceptions from Application_Error Event in Global.log
        -----------------------------------------------------------------------------------------------*/
        string strFileName = "Global";
        string strErrorLogPath = (HttpContext.Current.Server.MapPath(@"~\ErrorLogs\") + (strFileName + ".log"));
        string strErrorSrcFileName = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.CurrentExecutionFilePath);
        string strMethodName = new StackTrace().GetFrame(1).GetMethod().Name;
        string strUserName = HttpContext.Current.Session["ssnLoginName"] == null ? " " : HttpContext.Current.Session["ssnLoginName"] as string;
        string strErrorMessage;


        TextWriter twErrorFile;
        strErrorMessage = (DateTime.Now.ToString() + "\r\n"); ;
        strErrorMessage += "File Name     :" + strErrorSrcFileName + "\r\n";
        strErrorMessage += "Method Name   :" + strMethodName + "\r\n";
        strErrorMessage += "Error Text    :" + exception.Message + "\r\n";
        strErrorMessage += "Error Desc    :" + exception.StackTrace + "\r\n";
        strErrorMessage += "User Name     :" + strUserName + "\r\n";
        strErrorMessage += "-----------------------------------------------------------------------------------------------------------------------------------------------" + "\r\n";
        twErrorFile = File.AppendText(strErrorLogPath);
        twErrorFile.WriteLine(strErrorMessage);
        twErrorFile.Close();
    }

    public void LogError(string pStrClassName, string pStrFuncName, string pSrtErrText, string pStrStack, string strUserId)
    {

        try
        {

            string strFileName = DateTime.Today.ToString("dddd, MMMM dd, yyyy");
            string strErrorLogPath = (HttpContext.Current.Server.MapPath(@"~\ErrorLogs\") + (strFileName + ".log"));
            string strErrorSrcFileName = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.CurrentExecutionFilePath);
            string strMethodName = new StackTrace().GetFrame(1).GetMethod().Name;
            string strUserName = HttpContext.Current.Session["ssnLoginName"] == null ? " " : HttpContext.Current.Session["ssnLoginName"] as string;
            string strErrorMessage;


            TextWriter twErrorFile;
            strErrorMessage = (DateTime.Now.ToString() + "\r\n"); ;
            strErrorMessage += "File Name     :" + strErrorSrcFileName + "\r\n";
            strErrorMessage += "Method Name   :" + strMethodName + "\r\n";
            strErrorMessage += "Error Text    :" + pSrtErrText + "\r\n";
            strErrorMessage += "Error Desc    :" + pStrStack + "\r\n";
            strErrorMessage += "User Name     :" + strUserName + "\r\n";
            strErrorMessage += "-----------------------------------------------------------------------------------------------------------------------------------------------" + "\r\n";
            twErrorFile = File.AppendText(strErrorLogPath);
            twErrorFile.WriteLine(strErrorMessage);
            twErrorFile.Close();

        }
        catch (Exception ex)
        {
            string str1;
            str1 = ex.Message;
        }
    }
}
